﻿using System;
using System.Diagnostics;

namespace Game.Common.Resources
{
    public class GameResource
    {
        public event EventHandler Changed;

        readonly GameResourceType _Type;

        double _Value;
        
        public GameResource(GameResourceType type)
        {
            _Type = type;
        }

        public GameResource(
            GameResourceType type,
            double value)
        {
            _Type = type;
            _Value = value;
        }

        public GameResource(ReadonlyGameResource source)
        {
            _Type = source.Type;
            _Value = source.Value;
        }
        
        public GameResourceType Type
        {
            get { return _Type; }
        }

        public double Decrease(double amount)
        {
            if (amount > _Value)
            {
                amount = _Value;
            }

            _Value -= amount;
            Debug.Assert(_Value >= 0);

            FireChanged();
            return amount;
        }

        public double Increase(double amount)
        {
            _Value += amount;
            FireChanged();
            return amount;
        }

        internal void Set(double amount)
        {
            _Value = amount;
            FireChanged();
        }

        public double Value
        {
            get { return _Value; }
            set 
            { 
                _Value = value;
                FireChanged();
            }
        }

        void FireChanged()
        {
            if (Changed != null)
            {
                Changed(this, new EventArgs());
            }
        }
        
        public override string  ToString()
        {
            return "" + _Value;
        }

    }
}
